home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr53 / pctv4n_1.zip / QEFUNCS.C < prev    next >
C/C++ Source or Header  |  1993-06-10  |  7KB  |  330 lines

  1. /**************************************************
  2. * FILE NAME:  QEfuncs.c   TITLE:  SQL database API
  3. *
  4. * AUTHOR:  K. E. North II
  5. *
  6. * SYNOPSIS: 
  7. *
  8. *    application interface to Pioneer's Q+E Lib
  9. *
  10. ***************************************************/
  11.  
  12. #undef DEBUG    
  13.  
  14. #include <windows.h>
  15. #include <stdio.h>
  16. #include <dos.h>
  17. #include <stdlib.h>
  18. #include <conio.h>
  19. #include <string.h>
  20. #include <alloc.h>
  21.  
  22. #ifndef __QEDEFS_H
  23.         /*QELib-specific definitions */
  24. #include "QEdefs.h"
  25. #endif
  26.  
  27. #ifndef __QEAPI_H
  28.         /* function prototypes for QELib calls */
  29. #include "QEapi.h"
  30. #endif
  31.  
  32. #ifndef __QCOLUMN_H
  33.         /* QELib column description */
  34. #include "qcolumn.h"      
  35. #endif
  36.  
  37. #ifndef __DBTYPES_H
  38. #include "dbtypes.h"
  39. #endif
  40.  
  41. #ifndef __REQUEST_H
  42. #include "request.h"
  43. #endif
  44.  
  45. #ifndef __QEFUNCS_H
  46.         /* portable SQL API functions */
  47. #include "qefuncs.h"
  48. #endif
  49.  
  50.  
  51. /**************************************************
  52. * FUNCTION NAME: dbClose
  53. *
  54. * SYNOPSIS:
  55. *
  56. *  close a data base session - terminate connection
  57. *
  58. ***************************************************/
  59. int dbClose(CONNECTION si)
  60. {
  61. int        ExitStatus;
  62.  
  63.                             /* to logout */
  64.     ExitStatus = dbDisconnect( si );
  65.     if (ExitStatus)
  66.         return ExitStatus;
  67.     else 
  68.         return SQL_SUCCESS;
  69. }
  70.  
  71.  
  72. /**************************************************
  73. * FUNCTION NAME: dbRequest
  74. *
  75. * SYNOPSIS: 
  76. *
  77. *     prep/exec an SQL statement 
  78. *     for Q+E Lib
  79. *
  80. ***************************************************/
  81. int dbRequest( REQUEST rq, char *statement,
  82.                    CONNECTION si )
  83. {
  84.  
  85. HSTMT    hstmt;
  86. POINTER    st;
  87. int          StatementLength;
  88.  
  89.                     /* get length of */
  90.                     /* SQL command string */
  91.     StatementLength = strlen (statement);
  92.     if (StatementLength > MAXLEN)
  93.         {
  94.         rq->ExceedsMax=TRUE;
  95.         }
  96.     else rq->ExceedsMax = FALSE;
  97.  
  98.         /* copy 1st SQL string into request */
  99.     strcpy(rq->Statement,statement);
  100.  
  101.         /* build FAR * SQL request */
  102.     st = BuildRequest( rq );
  103.     rq->stptr = st;
  104.  
  105.     hstmt = ExecuteStatement( st, si );
  106.  
  107.         /* if there is an error, hstmt is  a 0     */
  108.         /* or negative number                     */
  109.     if (hstmt <= NOHANDLE)
  110.         {
  111.         return REQUEST_ERROR;
  112.         }
  113.     rq->hstmt = hstmt;    /* save request handle */
  114.     return SQL_SUCCESS;
  115. }
  116.  
  117. /**************************************************
  118. * FUNCTION NAME: BuildRequest
  119. *
  120. * SYNOPSIS: prepare an SQL request
  121. ***************************************************/
  122. POINTER BuildRequest( REQUEST rq )
  123. {
  124.  
  125. POINTER stptr;
  126.  
  127.     stptr = (char far *) farmalloc(MAXSQL);
  128.     movedata
  129.      (FP_SEG(rq->Statement), FP_OFF(rq->Statement),
  130.             FP_SEG(stptr), FP_OFF(stptr),
  131.             1+strlen(rq->Statement));
  132.     return stptr;
  133. }
  134.  
  135. /**************************************************
  136. * FUNCTION NAME: ExecuteStatement
  137. *
  138. * SYNOPSIS: executes a direct or prepared request
  139. **************************************************/
  140. HSTMT ExecuteStatement( POINTER stptr, 
  141.                         CONNECTION si )
  142. {
  143. HSTMT    hstmt;
  144. int        Status;
  145.  
  146.     hstmt = qeExecSQL (si->hdbc, stptr);
  147.     if (hstmt == 0)
  148.         {
  149.         Status = qeErr();
  150.         return Status;
  151.         }
  152.     return hstmt;
  153. }
  154.  
  155. /**************************************************
  156. * FUNCTION NAME: dbDescribe
  157. *
  158. * SYNOPSIS:     
  159. *     get column description info
  160. *
  161. ***************************************************/
  162. int dbDescribe(int ColumnPos,
  163.                 struct ColumnDesc *col,
  164.                 REQUEST rq)
  165. {
  166. char    far *name;
  167. int      Status;
  168.  
  169.     col->namelen = NAMELENGTH;
  170.     col->data_type = 0;
  171.     col->length = 0;
  172.     col->scale = 0;
  173.     strcpy(col->fldname,"   ");
  174.  
  175.          /* get the column name, type, width, etc. */
  176.     name = qeColName ( rq->hstmt, ColumnPos );
  177.     Status = qeErr();
  178.     if (Status == SQL_SUCCESS)
  179.         {
  180.         strcpy(col->fldname,name);
  181.         }
  182.     else return Status;
  183.  
  184.                 /* get the column type */
  185.     col->data_type = qeColType ( rq->hstmt, 
  186.                     ColumnPos );
  187.     Status = qeErr();
  188.     if (Status != SQL_SUCCESS)
  189.         return Status;
  190.  
  191.                 /* get the column width */
  192.     col->length = qeColWidth ( rq->hstmt,
  193.                             ColumnPos );
  194.     Status = qeErr();
  195.     if (Status == SQL_SUCCESS)
  196.         {
  197.         if (col->data_type == CHARACTER)
  198.             --col->length;
  199.         }
  200.     else return Status;
  201.  
  202.     if (col->data_type == DECIMAL)
  203.         {
  204.         col->scale = qeColScale ( rq->hstmt,
  205.                             ColumnPos );
  206.         Status = qeErr();
  207.         if (Status != SQL_SUCCESS)
  208.             return Status;
  209.         }
  210.  
  211.     return SQL_SUCCESS;
  212. }
  213.  
  214. /**************************************************
  215. * FUNCTION NAME: dbConnect
  216. *
  217. * SYNOPSIS:
  218. *             open an database connection
  219. ***************************************************/
  220.  
  221. int dbConnect( CONNECTION si, DATASRC dsrc)
  222. {
  223. int        Status;
  224.  
  225.                 /********************************/
  226.                 /*   Connect to Q+E database    */
  227.                 /********************************/
  228.  
  229.     si->cstr = BuildConnectString(dsrc);
  230.  
  231.             /* QELib connect */
  232.     si->hdbc  = qeConnect (si->cstr);
  233.     Status = qeErr();
  234.     if (si->hdbc == 0)
  235.       {
  236.       printf ("<Connect> \
  237.         qeConnect failure, Status: %d\n", Status);
  238.       return Status;
  239.       };
  240.     return SQL_SUCCESS;
  241. }
  242.  
  243.  
  244. /**************************************************
  245. * FUNCTION NAME: dbDisconnect
  246. *
  247. * SYNOPSIS: 
  248. *        terminate a data base connection
  249. ***************************************************/
  250. int dbDisconnect(CONNECTION si)
  251. {
  252. int      Status;
  253.  
  254.             // QELib disconnect
  255.     Status = qeDisconnect(si->hdbc);
  256.     if (Status)
  257.         return Status;
  258.     else
  259.         return SQL_SUCCESS;
  260.  
  261. }
  262.  
  263. /**************************************************
  264. * FUNCTION NAME: BuildConnectString
  265. *
  266. * SYNOPSIS: build connection string for DBF
  267. ***************************************************/
  268. POINTER BuildConnectString(DATASRC dsrc)
  269. {
  270.     char far *conec;
  271.     char *str="DRV=";
  272.  
  273.       /* to connect to a DBF database, supply only
  274.             the driver parameter */
  275.     strcat(str,dsrc->DBDriver);
  276.  
  277.     conec = (char far *) farmalloc(CONECLEN);
  278.     movedata(FP_SEG(str), FP_OFF(str),
  279.         FP_SEG(conec), FP_OFF(conec),
  280.         1+strlen(str));
  281.     return conec;
  282. }
  283.  
  284. /**************************************************
  285. * FUNCTION NAME: DecodeQEDataType
  286. *
  287. * SYNOPSIS: return description of data type
  288. ***************************************************/
  289.  
  290. char *DecodeQEDataType(int DataType)
  291. {
  292. static char Type[20];
  293.  
  294.     strcpy(Type,"Unknown");
  295.     if (DataType == 1)
  296.         strcpy(Type,"character");
  297.     if (DataType == 2)
  298.         strcpy(Type,"variable len char");
  299.     if (DataType == 3)
  300.         strcpy(Type,"decimal");
  301.     if (DataType == 4)
  302.         strcpy(Type,"long integer");
  303.     if (DataType == 5)
  304.         strcpy(Type,"integer");
  305.     if (DataType == 6)
  306.         strcpy(Type,"float");
  307.     if (DataType == 7)
  308.         strcpy(Type,"double");
  309.     if (DataType == 8)
  310.         strcpy(Type,"date-time");
  311.     return Type;
  312. }
  313.  
  314. /**************************************************
  315. * FUNCTION NAME: FindNumberofColumns
  316. *
  317. * SYNOPSIS: 
  318. *    get the number of columns in the request
  319. ***************************************************/
  320.  
  321. int FindNumberOfColumns( HSTMT hstmt )
  322. {
  323. int        n;
  324.  
  325.     n = qeNumCols (hstmt);
  326.     return n;
  327. }
  328.  
  329.  
  330.